/*--------------------------------------------------------------------------
  Project::MONDIALI SCI ALPINISMO
  Package::Gui
  Class::Gui
  File Gui.h
  --------------------------------------------------------------------------
  * METHODS AND CLASS PARAMETERS


  --------------------------------------------------------------------------
  * FUNCTIONS DESCRIPTION
  
	...

  --------------------------------------------------------------------------
  * COPYRIGHT

  --------------------------------------------------------------------------
  * INFO ABOUT AUTHOR AND VERSIONS
  Author:	Tino Giorgio	Data: 
  Rev1:			Data:

  --------------------------------------------------------------------------
  * E-MAIL & CONTACTS

  GIORGIO TINO:	tinoshi@libero.it
	           	shapeofangels@hotmail.com
	            giorgio.tino@studenti.polito.it

  --------------------------------------------------------------------------*/

#if !defined(BMPIMAGE_H)
#define BMPIMAGE_H

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <stdio.h>
#include <stdlib.h>

#include "Bitmap.h"

/* OpenGL Includes */
#ifdef FREEGLUT_IS_PRESENT
#  include <GL/freeglut.h>
#else
#  ifdef __APPLE__
#    include <GLUT/glut.h>
#  else
#    include <GL/glut.h>
#  endif
#endif

/* PLIB Includes */
#include <plib/ul.h>
#include <plib/pw.h>

/* DEFINES */
#define ZOOM_FACTOR 0.01f
#define MOVE_FACTOR 0.01f

#define HORIZONTAL 0 
#define VERTICAL 1
#define SQUARED 2

#define WIDGET_BORDER_X 0.01f
#define WIDGET_BORDER_Y 0.01f

#define WIDGET_BORDER_COLOR 0.0f,0.9f,0.9f,0.7f
#define WIDGET_SELECTED_COLOR 0.0f,0.9f,0.7f,0.8f
#define WIDGET_MOUSE_OVER_COLOR 0.7f,0.7f,0.9f,0.9f

class BMPImage  
{
public:
	BMPImage();
	BMPImage(float x_pos, float y_pos, float x_dim, float y_dim);
	BMPImage(float*** matrix,int h, int w,float x_pos, float y_pos, float x_dim, float y_dim);
	virtual ~BMPImage();
	
	int load(const char *filename);
	int save(const char *filename);
	void print();
	void display();
	
	void setPosition(float x, float y);
	
	void zoomIn(int z);
	void zoomOut(int z);
	void moveUp(int u);
	void moveDown(int d);
	void moveLeft(int l);
	void moveRight(int r);
	
	void mouse(int b,int s,int x,int y);

	int selected();
	
private:
    GLubyte          *bits;        /* Bitmap pixel bits */
    GLubyte          *image;       /* Image map for texture */
    GLubyte          *backImage;    /* Image on which use SubImage */
    int              bitsize;      /* Size of bitmap */
    int              infosize;     /* Size of header information */
    int              size;         /* Size of file */
    BITMAPFILEHEADER header;       /* File header */
    BITMAPINFO       *info;
    char             fileName[1024];    /* Bitmap file name */
    GLuint           texName;
    
    int mouseIsOver;
    int isSelected;
    
    int h,w;                 // image size in pixels
    int bh,bw;               // tesxture size (pow of 2) in pixels
    
    // Next couples of values are in %. One of the two is 1.0f, the
    // other is proportional
    float widget_x_dim;      
    float widget_y_dim;
    
    float image_x_dim;
    float image_y_dim;
    
    float texture_x_dim;
    float texture_y_dim;
    
    float screen_x_dim;
    float screen_y_dim;
    
    // Can be VERTICAL, HORIZONTAL, SQUARED
    int texture;
    int widget;
    int imagine;
    int screen;
    
    // Used to fit start image so that one side is as big as widget.
    float fit_dim;
    
    // sub texture dimensions
    float t_x_dim;
    float t_y_dim;
    
    void setScreenDimensions();
    void setImageDimensions();
    void setWidgetDimensions();
    void setTextureDimensions();
    void fitDimensions();
    
    // widget position
    float start_x;
    float start_y;
    
    // subtexture position
    float T_start_x;
    float T_start_y;
    
    // zoom factor
    float zoom;
    
    // Next couples of values are in %. One of the two is 1.0f, the
    // other is proportional
    float ww; // widget heigth
    float wh;

    int createTexture();
	int createTexture(float*** m, int he, int wi);
    int getNextPowTwo(int n);
    GLubyte* createPrintableBMP();
    
    // Value of x increment when zooming
    float zoom_x_inc;
    
    // Value of x increment when moving
    float move_x_inc;
    
    // Cumulative x increment
    float total_x_inc;
    float total_zoom_x_inc;
    float total_move_x_inc;
    
};

#endif // !defined(BMPIMAGE_H_)
